from sys import exit, argv
from string import join

print 'antiskrembel.py -- @skrembelbot word descrambler'

if len(argv) < 2:
    print "Usage: %s <scrambled word>" % argv[0]
    exit(1)

scrambled_word = argv[1]

print "Scrambled word: %s" % scrambled_word

word_list = {}
input_file = open('wordlist.txt', 'r')
while 1:
    word = input_file.readline()
    if not word:
        break
    word = word.rstrip("\n").lower()
    if len(word) != len(scrambled_word):
        continue
    chars = list(word)
    chars.sort()
    chars_sorted = join(chars, '')
    if not chars_sorted in word_list:
        word_list[chars_sorted] = set()
    word_list[chars_sorted].add(word)
input_file.close()

chars = list(scrambled_word)
chars.sort()
chars_sorted = join(chars, '')
if not chars_sorted in word_list:
    print 'Word not found!'
    exit(1)
word_list[chars_sorted].add(scrambled_word)

print 'Word(s) found: ',

for value in word_list[chars_sorted]:
    if value != scrambled_word:
        print '%s' % str(value),

exit(0)
